// Copyright (c) 2014 Morten Bakkedal
// This code is published under the MIT License.

using System;
using System.Collections.Generic;
using System.Diagnostics;

using Atomic.Libraries.Mathematics.Functions.Compilation;

namespace Atomic.Libraries.Mathematics.Functions
{
	[Serializable]
	[DebuggerDisplay("{Constant}")]
	public class ConstantFunction : Function
	{
		public ConstantFunction(double constant)
		{
			Constant = constant;
		}

		protected override double ComputeValue(IEvaluator evaluator)
		{
			return Constant;
		}

		protected override Function ComputeDerivative(Variable variable)
		{
			return 0.0;
		}

		protected override Function ComputePartialValue(IPartialEvaluator evaluator)
		{
			return this;
		}

		public override Expression Compile(CodeGenerator generator)
		{
			return Constant;
		}

		/*public static bool TryCast(Function function, out double constant)
		{
			if (function is ConstantFunction)
			{
				constant = ((ConstantFunction)function).Constant;
				return true;
			}

			if (function.IsZero)
			{
				constant = 0.0;
				return true;
			}

			constant = double.NaN;
			return false;
		}*/
		
		public double Constant
		{
			get;
			private set;
		}
	}
}
